<?php

class AdPositionControl {
	
	private $db;
	public function __construct() {
		$this->db = $GLOBALS['db'];
	}

	/**
	* 增加或者修改ad_position
	*
	* @param   string      $type            操作类型
	* @param   string      array     	      
	* @param   text        $content         留言内容
	* @return  int
	*/
	public function  operate_ad_position($type,$array = '',$w_type) {
		
		//调用存储过程
		$this->db->query("call operate_ad_position('{$array['position_id']}', '{$array['position_name']}', '{$array['position_size']}',
														'{$array['position_type']}', '{$array['position_description']}', '{$array['position_location']}',
														'{$_SESSION['AName']}', {$w_type}, @last_id);"
							);
		return array(	'affect_nums' => $this->db->getOne("SELECT  @inset_num "),
						'last_id' => $this->db->getOne("SELECT  @last_id ")
					);
	}


	/**
	* 得到分页相关信息
	*
	* @param   int        $page       页码
	* @param   int        $type       类型
	* @param   int        $w_type     网站类型
	* @param   int        $pagesize   取多少条记录
	* @return  array
	*/
	public function getUrlInfo($page, $type, $pagesize = 10, $w_type) {
		
		$sql = "SELECT COUNT(*) AS total 
				FROM ad_position  
				WHERE   ad_web  = {$w_type}  AND is_delete = 0 ";
		$page_info = get_page_info($sql, $pagesize, $page);
		$page_url = get_page_url($page, $page_info['page_num']);
		return  array('page_info' => $page_info, 'page_url' => $page_url);
	}
	
	/**
	* 得到列表ad_position信息
	*
	* @param   string     $type       类型（list为列表 不带搜索 而 list_s 在列表 带有搜索）
	* @param   int        $position_id   广告位主键
	* @param   int        $start      从那行开始
	* @param   int        $pagesize   取多少条记录
	* @param   int        $w_type     网站类型
	* @return  array
	*/
	public function  getAdPositionInfo($type, $position_id, $w_type, $start = 0, $pagesize = 10) {
		
		$rows  = array();
		
		if( $type == 'list_index') {//不带条件搜索
			$sql = "SELECT position_id, position_name, position_size, position_description, update_user,
							IF(update_time > 0, FROM_UNIXTIME(update_time, '%Y-%m-%d'), '') update_time 
					FROM ad_position 
					WHERE ad_web  = {$w_type}  AND is_delete = 0  
					ORDER BY position_id DESC 
					LIMIT {$start}, {$pagesize} ";
			$rows = $this->db->getAll($sql);
			
		} else if ($type == 'position_edit') {//根据ID得到相关信息
			$sql = "SELECT position_id, position_name, position_type, position_size, position_description, position_location, update_user,
							IF(update_time > 0, FROM_UNIXTIME(update_time, '%Y-%m-%d'), '') update_time 
					FROM ad_position 
					WHERE position_id  = {$position_id} AND is_delete = 0 
					LIMIT 1  ";
			$rows = $this->db->getOneRs($sql);
		}
		
		return $rows;
	}
	
	/**
	* @函数isPositionNameExist判断广告位名称是否存在
	* @param  NULL
	* @return  string
	*/
	public function isPositionNameExist($position_id, $position_name, $w_type) {
		
		
		$where_str = !empty($position_id) ? " AND  position_id != {$position_id}" : '';
				
		$sql = "SELECT count(*) AS nums  
				FROM ad_position 
				WHERE position_name = '{$position_name}' {$where_str} AND is_delete = 0 AND ad_web = {$w_type}";
		$nums = $this->db->getOne($sql);
		return  (int) $nums;
	}


}


?>